package wujinye.com.utils;


import wujinye.com.annotation.Table;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

// 反射工具类
public class RefUtil {

    // 返回SQL语句参数数组
    public static Object[] getParams(Object object){
        Class<?> aClass = object.getClass();
        Field[] fields = aClass.getDeclaredFields();
        Object[] obj = new Object[fields.length];
        int count = 0;
        for (Field field : fields) {
            String name = field.getName();
            String methodName = "get"+name.substring(0,1).toUpperCase()+name.substring(1);
            try {
                Method method = aClass.getMethod(methodName);
                Object o = method.invoke(object);
                obj[count++]=o;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return obj;
    }

    // 获取model类对应的数据库表名
    public static String getTableName(Class c){
        Table annotation = (Table)c.getAnnotation(Table.class);
        if (null!=annotation){
            String tableName = annotation.value();
            return tableName;
        }
        return null;
    }
}
